package com.aiyige.page.globalsearch.adapter;

import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import com.aiyige.R;
import com.aiyige.page.globalsearch.model.SearchGroup;
import com.aiyige.page.globalsearch.util.SearchUtil;
import com.aiyige.utils.GlideUtil;
import com.aiyige.utils.StringUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.LinkedList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zhuyakun on 2017/12/11.
 */

public class DynamicAdapter extends BaseQuickAdapter<SearchGroup.Sample, BaseViewHolder> {

    public DynamicAdapter() {
        super(new LinkedList<SearchGroup.Sample>());
    }

    @Override
    protected void convert(BaseViewHolder helper, SearchGroup.Sample item) {
        switch (item.getType()) {
            case SearchUtil.TYPE_INFO:
                ((InfoViewHolder) helper).bindData(item);
                break;
            case SearchUtil.TYPE_VIDEO:
                ((VideoViewHolder) helper).bindData(item);
                break;
            case SearchUtil.TYPE_PHOTO:
                ((PhotoViewHolder) helper).bindData(item);
                break;
        }
    }

    @Override
    protected int getDefItemViewType(int position) {
        return mData.get(position).getType();
    }

    @Override
    protected BaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
        BaseViewHolder baseViewholder;
        switch (viewType) {
            case SearchUtil.TYPE_INFO:
                baseViewholder = new InfoViewHolder(getItemView(R.layout.global_search_info_item, parent));
                break;
            case SearchUtil.TYPE_VIDEO:
                baseViewholder = new VideoViewHolder(getItemView(R.layout.global_search_video_item, parent));
                break;
            case SearchUtil.TYPE_PHOTO:
                baseViewholder = new PhotoViewHolder(getItemView(R.layout.global_search_photo_item, parent));
                break;
            default:
                baseViewholder = new BaseViewHolder(getItemView(R.layout.wild_card_item, parent));
                break;
        }
        return baseViewholder;
    }


    public class VideoViewHolder extends BaseViewHolder {
        SearchGroup.Sample data;
        @BindView(R.id.coverIv)
        ImageView coverIv;
        @BindView(R.id.episodeTv)
        TextView episodeTv;
        @BindView(R.id.durationTv)
        TextView durationTv;
        @BindView(R.id.coverLayout)
        FrameLayout coverLayout;
        @BindView(R.id.avatarIv)
        ImageView avatarIv;
        @BindView(R.id.authorName)
        TextView authorName;
        @BindView(R.id.titleTv)
        TextView titleTv;
        @BindView(R.id.watchNumTv)
        TextView watchNumTv;

        public VideoViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public void bindData(SearchGroup.Sample item) {
            data = item;
            GlideUtil.with(itemView.getContext())
                    .loadCover(data.getCover())
                    .into(coverIv);
            GlideUtil.with(itemView.getContext())
                    .loadAvatar(data.getOwnerAvatar())
                    .into(avatarIv);
            authorName.setText(Html.fromHtml(data.getOwner()));
            episodeTv.setText(String.format(StringUtils.getString(R.string.total_episode), data.getStages()));
            durationTv.setText(StringUtils.formatMediaDurationMS(data.getDurationTotal()));
            watchNumTv.setText(String.format(StringUtils.getString(R.string.watch_num), StringUtils.formatNum(data.getViewCount())));
            titleTv.setText(Html.fromHtml(data.getTitle()));
        }


    }

    public class PhotoViewHolder extends BaseViewHolder {
        SearchGroup.Sample data;
        @BindView(R.id.coverIv)
        ImageView coverIv;
        @BindView(R.id.photoNumTv)
        TextView photoNumTv;
        @BindView(R.id.coverLayout)
        FrameLayout coverLayout;
        @BindView(R.id.avatarIv)
        ImageView avatarIv;
        @BindView(R.id.authorName)
        TextView authorName;
        @BindView(R.id.titleTv)
        TextView titleTv;
        @BindView(R.id.watchNumTv)
        TextView watchNumTv;

        public PhotoViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public void bindData(SearchGroup.Sample item) {
            data = item;
            Glide.with(itemView.getContext())
                    .load(data.getCover())
                    .apply(RequestOptions.placeholderOf(R.drawable.default_image_bg))
                    .into(coverIv);
            Glide.with(itemView.getContext())
                    .load(data.getOwnerAvatar())
                    .apply(RequestOptions.circleCropTransform())
                    .into(avatarIv);
            authorName.setText(Html.fromHtml(data.getOwner()));
            photoNumTv.setText(String.format(StringUtils.getString(R.string.total_photo_num), data.getStages()));
            watchNumTv.setText(String.format(StringUtils.getString(R.string.watch_num), StringUtils.formatNum(data.getViewCount())));
            titleTv.setText(Html.fromHtml(data.getTitle()));
        }
    }

    public class ViewHolder extends BaseViewHolder {
        SearchGroup.Sample data;
        @BindView(R.id.coverIv)
        ImageView coverIv;
        @BindView(R.id.coverLayout)
        FrameLayout coverLayout;
        @BindView(R.id.avatarIv)
        ImageView avatarIv;
        @BindView(R.id.authorName)
        TextView authorName;
        @BindView(R.id.titleTv)
        TextView titleTv;
        @BindView(R.id.watchNumTv)
        TextView watchNumTv;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public void bindData(SearchGroup.Sample item) {
            data = item;
            Glide.with(itemView.getContext())
                    .load(data.getCover())
                    .apply(RequestOptions.placeholderOf(R.drawable.default_image_bg))
                    .into(coverIv);
            Glide.with(itemView.getContext())
                    .load(data.getOwnerAvatar())
                    .apply(RequestOptions.circleCropTransform())
                    .into(avatarIv);
            authorName.setText(Html.fromHtml(data.getOwner()));
            watchNumTv.setText(String.format(StringUtils.getString(R.string.watch_num), StringUtils.formatNum(data.getViewCount())));
            titleTv.setText(Html.fromHtml(data.getTitle()));
        }
    }

    public class InfoViewHolder extends BaseViewHolder {
        SearchGroup.Sample data;
        @BindView(R.id.coverIv)
        ImageView coverIv;
        @BindView(R.id.coverLayout)
        FrameLayout coverLayout;
        @BindView(R.id.avatarIv)
        ImageView avatarIv;
        @BindView(R.id.authorName)
        TextView authorName;
        @BindView(R.id.titleTv)
        TextView titleTv;
        @BindView(R.id.watchNumTv)
        TextView watchNumTv;

        public InfoViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public void bindData(SearchGroup.Sample item) {
            data = item;
            Glide.with(itemView.getContext())
                    .load(data.getCover())
                    .apply(RequestOptions.placeholderOf(R.drawable.default_image_bg))
                    .into(coverIv);
            Glide.with(itemView.getContext())
                    .load(data.getOwnerAvatar())
                    .apply(RequestOptions.circleCropTransform())
                    .into(avatarIv);
            authorName.setText(Html.fromHtml(data.getOwner()));
            watchNumTv.setText(String.format(StringUtils.getString(R.string.watch_num), StringUtils.formatNum(data.getViewCount())));
            titleTv.setText(Html.fromHtml(data.getTitle()));
        }
    }
}
